<?php


namespace app\admin\controller;


use think\facade\Request;
use think\Db;


class ApplyAreaController extends BaseController
{
    //渲染主页面
    public function index()
    {
        $data = Db::name('download_info')->select();
        $this->assign('data2',$data);
        return $this->fetch();
    }

    //传递编辑的信息
    public function edit()
    {
        $id = intval($_GET['id']);
        $data3 = Db::name('download_info')->where('id',$id)->find();
        $this->assign('data3',$data3);
        return $this->fetch();
    }

    //删除
    public function deleteinfo()
    {
        $id = intval($_GET['id']);
        $data = Db::name('download_info')->where('id',$id)->delete();
        if ($data == 1 && $id!=0){
            echo $data.'删除成功！';
            header("Refresh:1;url=/ShirneCMS/src/public/admin/apply_area/index.html");
        }
        else{
            echo $data.'删除失败！';
            header("Refresh:1;url=/ShirneCMS/src/public/admin/apply_area/index.html");
        }
    }

    //渲染到添加界面
    public function add()
    {
        return $this->fetch();
    }

    //添加上传的文件
    public function addinfo()
    {
        $file = request()->file('image');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( '../public/uploads/manageInfo');
        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            echo $info->getExtension();
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getSaveName();
            // 输出 42a79759f284b767dfcb2a0197904287.jpg
            echo $info->getFilename();
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }

        $title = Request::post('title');
        $description = Request::post('description');
        $message = [
            'title' => $title,
            'describle' => $description,
            'create_time' => date('Y-m-d H:i:s'),
        ];
        $data = Db::name('download_info')->insert($message);
        echo $data.'添加成功';
        header("Refresh:1;url=/ShirneCMS/src/public/admin/apply_area/index.html");
    }

    //修改信息
    public function editinfo()
    {
        $id = Request::post('id');
        $title = Request::post('title');
        $description = Request::post('description');

        $message = [
            'title' => $title,
            'describle' => $description,
            'create_time' => date('Y-m-d H:i:s'),
        ];


        $data = Db::name('download_info')->where('id',$id)->update($message);
        echo $data;
        header("Refresh:1;url=/ShirneCMS/src/public/admin/apply_area/index.html");
    }

}